package ca.etsmtl.gti785.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import ca.etsmtl.gti785.classes.Application;
import ca.etsmtl.gti785.classes.Server;
import ca.etsmtl.gti785.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * This class is used to adapt a list of servers to an android list.
 * @see Server
 * @author Étienne Faucher
 */
@SuppressLint("SimpleDateFormat")
public class ServerAdapter extends ArrayAdapter<Server> 
{
	
	public ServerAdapter(Context context, int textViewResourceId) 
	{
	    super(context, textViewResourceId);
	}
	public ServerAdapter(Context context, int resource, List<Server> objects) 
	{
		super(context, resource, objects);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
	    View v = convertView;

	    if (v == null) 
	    {
	        LayoutInflater vi;
	        vi = LayoutInflater.from(getContext());
	        v = vi.inflate(R.layout.server_layout, null);
	    }

	    Server p = getItem(position);

	    if (p != null) 
	    {
	        RelativeLayout serverLayout = (RelativeLayout) v.findViewById(R.id.serverRelativeLayout);
	        if(serverLayout != null)
	        {
	        	if(p.IsSelected())
		    	{
	        		serverLayout.setBackgroundResource(R.drawable.serveritemselectedborder);
		    	}
		    	else
		    	{
		    		serverLayout.setBackgroundResource(R.drawable.serverlistborder);
		    	}
	        }

	        TextView serverDistance = (TextView) v.findViewById(R.id.server_distance);

	        if (serverDistance != null) 
	        {
	        	double latitude = Application.getInstanceOf().getGPSTracker().getLatitude();
	        	double longitude = Application.getInstanceOf().getGPSTracker().getLongitude();
	        	if(p.getLongitude() != Constants.Server_Default_Location && p.getLatitude() != Constants.Server_Default_Location)
	        	{
		        	double kilometers = DistanceCalculator.DistanceBetweenPlaces(p.getLongitude(), p.getLatitude(), longitude, latitude);
		        	DecimalFormat f = new DecimalFormat("##.00");
		        	
		        	serverDistance.setText(String.format(getContext().getResources().getString(R.string.serverDistanceAway), f.format(kilometers)));
	        	}
	        	else
	        	{
	        		serverDistance.setText(getContext().getResources().getString(R.string.serverDistanceNotFound));
	        	}
	        }
	        
	        TextView serverName = (TextView) v.findViewById(R.id.server_name);

	        if (serverName != null) 
	        {
	        	serverName.setText(p.getName());
	        }
	        
	        TextView serverLastAccess = (TextView) v.findViewById(R.id.server_last_access);

	        if (serverLastAccess != null) 
	        {
	        	serverLastAccess.setText(String.format(getContext().getResources().getString(R.string.serverLastAccessIs), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.GetLastAccessDate())));
	        }
	        
	        ImageView iv = (ImageView) v.findViewById(R.id.icon);
	        if(iv != null) 
	        { 
	        	if(p.IsOnline())
	        	{
	        		if(p.HasNotifications())
	        		{
			        	iv.setImageResource(R.drawable.server_on_notif);
	        		}
	        		else
	        		{
			        	iv.setImageResource(R.drawable.server_on);
	        		}
	        	}
	        	else
	        	{
		        	iv.setImageResource(R.drawable.server_off);
	        	}
	        }
	    }

	    return v;

	}

}
